package au.com.handsunited.web.controller.noticeboard;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import au.com.handsunited.model.BaseEntity;
import au.com.handsunited.service.EntityService;

/**
 * @author Arun Chandrasekhar
 *
 */
public class NoticeBoardViewController extends AbstractController {


	private EntityService entityService;
	private String successView;
	private String failureView;

	/**
	 *
	 */
	public NoticeBoardViewController() {

	}

	/**
	 * @see org.springframework.web.servlet.mvc.AbstractController#handleRequestInternal(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		String id = request.getParameter("id");

		ModelAndView mav = new ModelAndView();
		Map<String, Object> model = new HashMap<String, Object>();

		if (StringUtils.isNotBlank(id)) {
			BaseEntity entity = entityService.getEntity(id);
			if (entity != null) {
				model.put("entity", entity);
				mav.setViewName(successView);
				mav.addAllObjects(model);
				return mav;
			}
		}

		mav.setViewName(failureView);
		mav.addAllObjects(model);
		return mav;
	}

	/**
	 * @return the entityService
	 */
	public EntityService getEntityService() {
		return entityService;
	}

	/**
	 * @param entityService the entityService to set
	 */
	public void setEntityService(EntityService entityService) {
		this.entityService = entityService;
	}

	/**
	 * @return the successView
	 */
	public String getSuccessView() {
		return successView;
	}

	/**
	 * @param successView the successView to set
	 */
	public void setSuccessView(String successView) {
		this.successView = successView;
	}

}
